#!/usr/bin/perl

$sourceFile = $ARGV[0];
$depFile = $ARGV[1];
$alignFile = $ARGV[2];
$mode = $ARGV[3];

my $corpus = {};

&loadSen($corpus,'SSen',$sourceFile);
&loadDep($corpus,'Sdep',$depFile);
&loadAlign($corpus,'Align',$alignFile);

local $skey;

foreach $skey (sort {$a<=>$b} keys %$corpus) { 
	local %done = ();
	&getManyMany($corpus->{$skey},$corpus->{$skey}->{'Sdep'}->[0]->{'root'});
}

sub getManyMany {

	my($senCorpus,$root) = ($_[0],$_[1]);
	my($manyMany) = {};

	my @rootAlns = @{$senCorpus->{'Align'}->{'aln'}->[$root]};
#	print "Align ($root) =".join(',',@rootAlns)."\t\t\t";;

	if(!defined $done{$root}) {
		# Get Many Many Partners
		
		for($word=0;$word<@{$senCorpus->{'SSen'}};$word++) {
			my @myAlns = @{$senCorpus->{'Align'}->{'aln'}->[$word]};
			if(&matchArr(\@rootAlns,\@myAlns)) {
				$manyMany->{$word} = 1;
			}
		}
#		print "Matched: ".join(',',keys %manyMany)."\t\t\t";

		my @found = &scout($senCorpus,$root,$manyMany,0);
		if(scalar(@found)>1) {
#			print "Found ($root) ";
			foreach $fnd (@found) {
				my($level,$indx,$c) = split(/\s+/,$fnd);
				if($mode==0) {
					print "$level~".$senCorpus->{'SSen'}->[$indx]->{'word'}."/".$senCorpus->{'SSen'}->[$indx]->{'pos'}."~".eval($indx)." ";
				}
				elsif($mode==1) {
					print "$level~".$senCorpus->{'SSen'}->[$indx]->{'pos'}." ";
				}
				elsif($mode==2) {
					print "$level~".$senCorpus->{'SSen'}->[$indx]->{'pos'}."~".eval($indx-$root)." ";
				}
				elsif($mode==3) {
					print "$level ";
				}
				elsif($mode==4) {
					print "$level-$c ";
				}
				elsif($mode==5) {
					print "$level-".eval($indx-$root)." ";
				}
			}

			if(scalar(@found) != scalar(keys %$manyMany)) {
				my @notfound = ();
				foreach $oneNode (keys %$manyMany) {
					my $there = 0;
					foreach $f (@found) {
						my($level,$indx,$c) = split(/\s+/,$f);
						if($oneNode==$indx) {
							$there = 1;
						}	
					}	
					if($there==0) {
						push(@notfound,$oneNode);
					}
				}

				if($mode==0) {
					print "  Error!!! ($skey) ";
					foreach $indx (@notfound) {
						print "$level~".$senCorpus->{'SSen'}->[$indx]->{'word'}."/".$senCorpus->{'SSen'}->[$indx]->{'pos'}."~$indx ";
					}
				}
			}
			print "\n";
		}


		foreach $oneNode (keys %$manyMany) {
			$done{$oneNode} = 1;
		}
	}

	$done{$root} = 1;
	foreach $child (@{$senCorpus->{'Sdep'}->[$root]->{'attch'}}) {
		&getManyMany($senCorpus,$child);
	}

	
	
}

sub scout {
	my($senCorpus,$root,$manyMany,$level) = @_;

	my $node;
	my $child;
	my @found;


#	print "Alignments = ".join('-',@{$senCorpus->{'Align'}->{'aln'}->[$root]})."\n";

	if($manyMany->{$root}) {
	#if($manyMany->{$root} || @{$senCorpus->{'Align'}->{'aln'}->[$root]}==0) {

#	if(@{$senCorpus->{'Align'}->{'aln'}->[$root]}) {
#			$manyMany->{$root}="d";
#			$manyMany->{$root}=0;
			push(@found,"l$level $root ".scalar(@{$senCorpus->{'Sdep'}->[$root]->{'attch'}}));
			$done{$root} = 1;
#		}
#		else {
#			$manyMany->{$root}="*";
#			push(@found,"l$level *$root* ");
#			$done{$root} = 1;
#		}

		foreach $child (@{$senCorpus->{'Sdep'}->[$root]->{'attch'}}) {
			my @cFound = &scout($senCorpus,$child,$manyMany,$level+1);
			if($cFound[0] ne "NF") {
				push(@found,@cFound);
			}
		}

		return @found;
	}
	elsif(scalar(@{$senCorpus->{'Align'}->{'aln'}->[$root]})==0) {
		foreach $child (@{$senCorpus->{'Sdep'}->[$root]->{'attch'}}) {
			my @cFound = &scout($senCorpus,$child,$manyMany,$level+1);
			if($cFound[0] ne "NF") {
				push(@found,@cFound);
			}
		}

		return @found;
	}
	else {
		return "NF";
	}
}

sub matchArr {
	my($arr1,$arr2) = ($_[0],$_[1]);

	if(@$arr1==0 || @$arr2==0) {
		return 0;
	}

	foreach $elem1 (@$arr1) {
		foreach $elem2 (@$arr2) {
			if($elem1==$elem2) {
				return 1;
			}
		}
	}
	return 0;
}

sub loadSen {

	my($corpus) = shift;
	my($type) = shift;
	my($file) = shift;

	open(FILE,$file) || die "Could not open $file\n";

	my @lines = <FILE>;
	chomp(@lines);

	my $no_lines = @lines;
	my $senNum ;
	my $i=0;
	my $wordnum = 0;
	my $flag = "i";

	$senStart=1;
	if($flag eq "i")
	{
		for($i=0; $i < $no_lines; $i++)
		{
			if($lines[$i] =~ /^\s*$/)
			{
				$senStart=1;
			}
			elsif($senStart==1 && $lines[$i] =~ /^([0-9]+)$/)
			{
				$senNum = $1;
				$wordnum = 0;
				$senStart=0;
			}
			else
			{
				my($word_POS_root,@features) = split(/\t/,$lines[$i]);

				my($word,$pos,$root) = split(/_/,$word_POS_root);
#print "$senNum $type $word $pos $root\n";

				$corpus->{$senNum}->{$type}->[$wordnum]->{'word'} = $word;
				$corpus->{$senNum}->{$type}->[$wordnum]->{'pos'} = $pos;
				$corpus->{$senNum}->{$type}->[$wordnum]->{'root'} = $root;

# For 'prep' and 'aux' of the word. 

				my $feature;
				foreach $feature (@features)
				{
					my ($feat,$value) = split(/:/,$feature); 
					$corpus->{$senNum}->{$type}->[$wordnum]->{$feat} = $value;
				}
				$wordnum++;
			}
		}
	}
	else
	{
		for($i=0; $i < $no_lines; $i++)
		{
#                       print $lines[$i]."\n";
			$senNum = $i + 1;
			$wordnum = 0;
			@words = split(/\s+/,$lines[$i]);
			foreach $word_POS_root (@words)
			{
				my($word,$pos,$root) = split(/_/,$word_POS_root);
				$corpus->{$senNum}->{$type}->[$wordnum]->{'word'} = $word;
				$corpus->{$senNum}->{$type}->[$wordnum]->{'pos'} = $pos;
				$corpus->{$senNum}->{$type}->[$wordnum]->{'root'} = $root;

				$wordnum++;
			}
		}

	}
}

sub loadDep {

	my($corpus) = shift;
	my($type) = shift;
	my($depfname) = shift;
	my($senNum);
	
	open(DEPTREE,$depfname) || die "Can't open $depfname\n";
	my($depline);
	my($procsen) = 0;
	my($senidx,$rootpos) ;
	my($Senid) = 1;

	while($depline=<DEPTREE>)
	{
		chomp($depline);
#		SRIRAM CHANGE Aug 07, 2008
		$senNum = eval($depline);

		my($Dep);

		my $line = <DEPTREE>;
		chomp($line);
		($temp,$root_pos) = split(/\s+/,$line);

		$Dep = &addroot($Dep,$root_pos);

		while(!($line=~/^\s*$/))
		{
			$line = <DEPTREE>; chomp($line);

			if($line=~/^[#](.*?) (.*)$/)
			{
				$word_num = $1;
				$Dep = &createnode($Dep,$word_num);

				$line = <DEPTREE>;  chomp($line);
			}
			elsif($line=~/pos: (.*)/) {
				$Dep = &addpos($Dep,$word_num,$1);
			}
			elsif($line=~/rel: (.*)/) {
				$Dep = &addrel($Dep,$word_num,$1);
			}

			elsif($line=~/att #(.*)/)
			{
				$Dep = &addattch($Dep,$word_num,$1);
			}
			if($line=~/^[&](.*?)$/)   #Ask Karthik to check this
			{
				$word_num = $1;

				$line = <DEPTREE>;  chomp($line);

				$Dep = &createnode($Dep,$word_num);
			}
			elsif($line=~/con #(.*)/)
			{
				$Dep = &addattch($Dep,$word_num,$1);
			}
		}

		$corpus->{$senNum}->{$type} = $Dep;
	}
}

sub addroot {
	my($this) = shift;
	$this->[0]->{'root'} = shift;
	return $this;
}

sub createnode {
	my($this) = shift;
	my($word_num) = shift;

	$this->[$word_num]->{'noatt'} = 0;
	return $this;
}

sub addrel {
	my($this) = shift;
	my($relation) = shift;

	$this->[$word_num]->{'rel'} = $relation;
	return $this;
}

sub addpos {
	my($this) = shift;
	my($partOfSpeech) = shift;

	$this->[$word_num]->{'pos'} = $partOfSpeech;
	return $this;
}

sub addattch {
	my($this) = shift;
	my($word_num) = shift;
	
	$this->[$word_num]->{'noatt'}++;
	push(@{$this->[$word_num]->{'attch'}},$1);	
	return $this;
}

sub loadAlign {
	my($corpus) = shift;
	my($type) = shift;
	my($file) = shift;
	my($strength) = shift;

	open(FILE,$file) || die "Can't open $file\n";
	my @lines = <FILE>;

	my($line);
	my $no_lines = @lines;
	my $senNum;
	my $i=0;
	my $wordnum = 0;
	my $alignments;
	
	for($i=0; $i < $no_lines; $i++)
	{
		$line = $lines[$i]; 
		($senNum,$srcword,$tgtword,$strength) = split(/\s+/,$line);
		if($strength==0) {
			$alignments->{$senNum}->{"$srcword $tgtword"} = 1;
		}

	}

	foreach $senNum (keys %{$alignments}) {
		my $Align;

		foreach $line (keys %{$alignments->{$senNum}}) {
			($srcword,$tgtword) = split(/\s+/,$line);
			$Align = &addalign($Align,$srcword,$tgtword);
		}

		$corpus->{$senNum}->{$type} = $Align; 

	}
}

sub addalign {
	my($Align) = shift;
	my($srcword) = shift;
	my($tgtword) = shift;

	push(@{$Align->{'aln'}->[$srcword]},$tgtword);
	return $Align;
}

